package com.sample.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.FamilyForm;
import com.sample.javabean.Family;
import com.sample.util.Pager;

/**
 * 
 * @author 演示
 * 
 */
public class FamilyAction extends BaseAction {

	private final static Log log = LogFactory.getLog(FamilyAction.class);
	private FamilyForm familyForm = new FamilyForm();
	private Integer id;//定义变量id
	private Pager<Family> pageList;
	private String keywords;
	
	// 使用new()操作符实例化领域模型
	private Family family = new Family();

	/**
	 * 添加
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add()throws Exception{
		try{
			log.info("add Family:"+familyForm.getName());
			if(familyForm!=null){
				family.setCityno(familyForm.getCityno());
				family.setZhenno(familyForm.getZhenno());
				family.setZuno(familyForm.getZuno());
				family.setJiano(familyForm.getJiano());
				family.setName(familyForm.getName());
				family.setIdcard(familyForm.getIdcard());
				family.setCunno(familyForm.getCunno());
				SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
				String time = format.format(new Date());
				family.setTime(time);
				family.addFamily();	
			}
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	/**
	 * 显示记录列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listFamily() throws Exception {
		try {

			pageList = family.list(page, size, familyForm,keywords);
			return "listFamily";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	public String inputFamily() throws Exception {
		return "inputFamily";
	}

	public String editFamily() throws Exception {
		try {
			family.setId(id);
			family.load();
			return "editFamily";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		try {
			if (familyForm != null) {
				family.setId(familyForm.getId());
				family.setCityno(familyForm.getCityno());
				family.setZhenno(familyForm.getZhenno());
				family.setZuno(familyForm.getZuno());
				family.setJiano(familyForm.getJiano());
				family.setName(familyForm.getName());
				family.setIdcard(familyForm.getIdcard());
				family.setCunno(familyForm.getCunno());
				SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
				String time = format.format(new Date());
				family.setTime(time);

				family.editFamily();
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		try {
			if (familyForm != null) {
				family.setId(id);
				family.delFamily();
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public FamilyForm getFamilyForm() {
		return familyForm;
	}

	public void setFamilyForm(FamilyForm familyForm) {
		this.familyForm = familyForm;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Pager<Family> getPageList() {
		return pageList;
	}

	public void setPageList(Pager<Family> pageList) {
		this.pageList = pageList;
	}

	public Family getFamily() {
		return family;
	}

	public void setFamily(Family family) {
		this.family = family;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
	
}
